#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jerasure-2.h"
#include "reed_sol-2.h"

#define talloc(type, num) (type *) malloc(sizeof(type)*(num))

void usage(char *s)
{
  fprintf(stderr, "usage: reed_sol_01 k m w - Does a simple Reed-Solomon coding example in GF(2^w).\n");
  fprintf(stderr, "       \n");
  fprintf(stderr, "       w must be 8, 16 or 32.  k+m must be <= 2^w.  It sets up a classic\n");
  fprintf(stderr, "       Vandermonde-based distribution matrix and encodes k devices of\n");
  fprintf(stderr, "       %ld bytes each with it.  Then it decodes.\n", sizeof(long));
  fprintf(stderr, "       \n");
  fprintf(stderr, "This demonstrates: JER_Matrix_Encode()\n");
  fprintf(stderr, "                   JER_Matrix_Decode()\n");
  fprintf(stderr, "                   JER_Matrix::Print()\n");
  fprintf(stderr, "                   reed_sol_vandermonde_coding_matrix()\n");
  if (s != NULL) fprintf(stderr, "%s\n", s);
  exit(1);
}

static void print_data_and_coding(JER_Slices *slices)
{
	int i, j, x;
	int n, sp;
	int k, m, w, size;
	uint64_t l;

	k = slices->K;
	m = slices->M;
	w = slices->W;
	size = slices->size;

	if (k > m) n = k;
	else n = m;
	sp = size * 2 + size/(w/8) + 8;

	printf("%-*sCoding\n", sp, "Data");
	for (i = 0; i < n; i++) {
		if (i < k) {
			printf("D%-2d:", i);
			for (j = 0; j < size; j+=(w/8)) {
				printf(" ");
				for (x = 0; x < w/8; x++){
					printf("%02x", (unsigned char)slices->ptrs[i][j+x]);
				}
			}
			printf("    ");
		}
		else printf("%*s", sp, "");
		if (i < m) {
			printf("C%-2d:", i);
			for (j = 0 ; j < size; j+=(w/8)) {
				printf(" ");
				for (x = 0; x < w/8; x++){
					printf("%02x", (unsigned char)slices->ptrs[i+k][j+x]);
				}
			}
		}
		printf("\n");
	}
	printf("\n");
}

int main(int argc, char **argv){
  uint64_t l;
	int sou;
  int k, w, i, j, m;
	JER_Matrix *jm;
	JER_Slices *slices;
	unsigned char *data;
	vector <int> erasures, erased;
	JER_Matrix *dm;
	int *dm_ids;
  
  if (argc != 4) usage(NULL);
  if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage((char *)"Bad k");
  if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage((char *)"Bad m");
  if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage((char *)"Bad w");
  if (w <= 16 && k + m > (1 << w)) usage((char *)"k + m is too big");

  jm = reed_sol_vandermonde_coding_matrix(k, m, w);
	sou = sizeof(uint64_t);

  printf("Last m rows of the Distribution Matrix:\n\n");
	jm->Print();
  printf("\n");

	slices = new JER_Slices();
	slices->W = w;
	slices->K = k;
	slices->M = m;
	slices->PS = sou;
	slices->size = sou;

  srand48(0);
	slices->ptrs.resize(k+m);
	data = new unsigned char [sou * (k + m)];
  for (i = 0; i < k; i++) {
		slices->ptrs[i] = data + i*sou;
    l = lrand48();
		l <<= 8*4;
		l += lrand48();
    memcpy(data + i*sou, &l, sou);
  }

	for (i = 0; i < m; i++) {
		slices->ptrs[i+k] = data + k*sou + i*sou;
	}

	JER_Matrix_Encode(slices, jm);
  
  printf("Encoding Complete:\n\n");
  print_data_and_coding(slices);

	erasures.resize(m);
	erased.resize(m+k);
  for (i = 0; i < m+k; i++) erased[i] = 0;
  l = 0;
  for (i = 0; i < m; ) {
    erasures[i] = lrand48()%(k+m);
    if (erased[erasures[i]] == 0) {
      erased[erasures[i]] = 1;
      memcpy(data + erasures[i]*sou, &l, sou);
      i++;
    }
  }

  printf("Erased %d random devices:\n\n", m);
  print_data_and_coding(slices);
  
	i = JER_Matrix_Decode(slices, jm, 1, erasures);

  printf("State of the system after decoding:\n\n");
  print_data_and_coding(slices);
  
  return 0;
}
